package cc.angis.xiugallery.db;

import java.util.ArrayList;
import java.util.List;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.provider.MediaStore;

public class FavouriteImagesDBHelper extends BaseDBHelper {
	
	public FavouriteImagesDBHelper(){
		
	}
	
	public FavouriteImagesDBHelper(Context context, String table){
		super(context, table);
	}
	
	public FavouriteImagesDBHelper(Context context, String table, ContentValues values){
		super(context, table, values);
	}
	
	public long delete(int favid){
		mWhereClaus = "favid=?";
		mWhereArgs = new String[] { String.valueOf(favid) };
		return delDB();
	}
	
	public long deleteIn(String imgids){
		mWhereClaus = "imgid IN (" + imgids + ")";
		mWhereArgs = null;
		return delDB();
	}
	
	public long delete(int favid, long imgid){
		mWhereClaus = "favid=? AND imgid=?";
		mWhereArgs = new String[] { String.valueOf(favid), String.valueOf(imgid) };
		return delDB();
	}
	
	
	public long insert(int favid, long imgid, String favname){
			mValues = new ContentValues();
			mValues.put(TableConstants.FavouriteImagesColumn.FAVID, favid);
			mValues.put(TableConstants.FavouriteImagesColumn.IMGID, imgid);
//			lContentValues.put(TableConstants.FavouriteImagesColumn.FAVNAME, favname);
			return insertDB();
	}
	
	public List<Long> getAllByFavId(int favid){
		List<Long> imgs = new ArrayList<Long>();
		Cursor cursor = mDBHelper.getReadableDatabase().query(mTable, new String[]{TableConstants.FavouriteImagesColumn.IMGID}, "favid=?", new String[]{ String.valueOf(favid)}, null, null, TableConstants.FavouriteImagesColumn.ID);
		int imageIdColumn = cursor.getColumnIndex(
				TableConstants.FavouriteImagesColumn.IMGID);
		if (cursor.moveToFirst()){
			do {
				imgs.add(cursor.getLong(imageIdColumn));
			}while(cursor.moveToNext());
		}
		cursor.close();
		return imgs;
	}
	
	public List<Long> getAllFav(){
		List<Long> imgs = new ArrayList<Long>();
		Cursor cursor = mDBHelper.getReadableDatabase().query(mTable, new String[]{TableConstants.FavouriteImagesColumn.IMGID}, null, null, TableConstants.FavouriteImagesColumn.IMGID, null, TableConstants.FavouriteImagesColumn.ID);
		int imageIdColumn = cursor.getColumnIndex(
				TableConstants.FavouriteImagesColumn.IMGID);
		if (cursor.moveToFirst()){
			do {
				imgs.add(cursor.getLong(imageIdColumn));
			}while(cursor.moveToNext());
		}
		cursor.close();
		return imgs;
	}
	
	public long getLastImageId(int favid){
		long lastImgId = -1;
		Cursor cursor = mDBHelper.getReadableDatabase().query(mTable, new String[]{TableConstants.FavouriteImagesColumn.IMGID}, "favid=?", new String[]{ String.valueOf(favid)}, null, null, TableConstants.FavouriteImagesColumn.ID, "1");
		int imageIdColumn = cursor.getColumnIndex(
				TableConstants.FavouriteImagesColumn.IMGID);
		if (cursor.moveToFirst()){
			do {
				lastImgId = cursor.getLong(imageIdColumn);
			}while(cursor.moveToNext());
		}
		cursor.close();
		return lastImgId;
	}
}
